package com.hs.security.service.impl;

import com.hs.security.exception.ProjectException;
import com.hs.security.mapper.TbUserMapper;
import com.hs.security.pojo.LoginUser;
import com.hs.security.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @ClassName UserDetailsServiceImpl
 * @Description
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private TbUserMapper mapper;


    /**
     * 用户认证的方法
     * @param username
     * @return org.springframework.security.core.userdetails.UserDetails
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TbUser user = mapper.selectByUsername(username);
        if (Objects.isNull(user)) {
           throw new ProjectException("用户名或密码错误", 401);
        }
        return new LoginUser(user);
    }
}
